package cn.byapps.attendance.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import cn.byapps.attendance.dao.StudentDao;
import cn.byapps.attendance.entity.Clazz;
import cn.byapps.attendance.entity.Dept;
import cn.byapps.attendance.entity.Dorm;
import cn.byapps.attendance.entity.Student;
import cn.byapps.attendance.entity.XlsStudent;
import cn.byapps.attendance.service.StudentService;
import cn.byapps.attendance.util.DataTablesResult;
import cn.byapps.attendance.util.Pager;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

	@Resource
	private StudentDao studentDao;

	@Override
	public List<Student> findAll() {
		return studentDao.findAll();
	}

	@Override
	public void addPerson(Student p) {
		studentDao.add(p);
	}

	@Override
	public Student getById(String pid) {
		return studentDao.get(pid);
	}

	@Override
	public void updatePerson(Student p) {
		studentDao.update(p);
	}

	@Override
	public void deleteById(String pid) {
		Student student = new Student();
		student.setId(pid);
		studentDao.delete(student);
	}

	@Override
	public List<Student> find(int curPage, int pageCount) {
		return studentDao.find((curPage - 1) * pageCount, pageCount);
	}

	@Override
	public int countAll() {
		return (int) studentDao.count();
	}

	@Override
	public Pager<Student> getPage(String strCurPage, String strPageCount) {

		Pager<Student> page = new Pager<Student>();

		if (strCurPage == null || strPageCount == null) {
			strCurPage = "1";
			strPageCount = "5";
		}
		// 计算总页数，修正当前页的值
		int countAll = (int) studentDao.count();
		int curPage = Integer.parseInt(strCurPage);
		int pageCount = Integer.parseInt(strPageCount);
		int totalPage = (countAll + pageCount - 1) / pageCount;
		if (curPage <= 0) {
			curPage = 1;
		}
		if (curPage > totalPage) {

			if (totalPage > 0)
				curPage = totalPage;
			else
				curPage = 1;
		}

		List<Student> persons = studentDao.find((curPage - 1) * pageCount, pageCount);
		page.setLists(persons);
		page.setCurPage(curPage);// 查询的页数
		page.setPageCount(pageCount);// 查询行数
		page.setTotalPage(totalPage);// 总页数
		page.setCountAll(countAll);// 总行数

		return page;
	}

	@Override
	public List<XlsStudent> getXLSData(String dept, String clazz) {
		// TODO Auto-generated method stub
		Map<String, Object> maps = new HashMap<String, Object>();
		List<Student> students = null;
		if (clazz != null && !"".equals(clazz)) {
			maps.put("clazz", clazz);
			students = studentDao.findAll(maps);
		} else if (dept != null && !"".equals(dept)) {
			maps.put("dept", dept);
			students = studentDao.findAll(maps);
		} else {
			students = studentDao.findAll();
		}
		if (students != null) {
			List<XlsStudent> std = new ArrayList<XlsStudent>();
			for (Student s : students) {
				XlsStudent xls = new XlsStudent();
				xls.setId(s.getId());
				xls.setName(s.getName());
				xls.setDept(s.getDept());
				xls.setClazz(s.getClazz());
				if (s.getFavJob() != null) {
					xls.setCellphone(s.getFavJob().getCellphone());
					if (s.getFavJob().getFirstJob() != null)
						xls.setFirst(s.getFavJob().getFirstJob().getName());
					if (s.getFavJob().getSecondJob() != null)
						xls.setSecond(s.getFavJob().getSecondJob().getName());
					if (s.getFavJob().getThirdJob() != null)
						xls.setThird(s.getFavJob().getThirdJob().getName());
				} else {
					xls.setCellphone(s.getCellphone());
				}
				std.add(xls);
			}
			return std;
		}
		return null;
	}

	@Override
	public List<Clazz> findClazzes() {
		List objs = studentDao.list("clazz");
		List<Clazz> clazzes = new ArrayList<Clazz>();
		for (Object obj : objs) {
			if (obj != null) {
				Clazz c = new Clazz();
				c.setName((String) obj);
				clazzes.add(c);
			}
		}
		return clazzes;
	}

	@Override
	public List<Dorm> findDorms() {
		List objs = studentDao.list("dorm");
		List<Dorm> clazzes = new ArrayList<Dorm>();
		for (Object obj : objs) {
			if (obj != null) {
				Dorm c = new Dorm();
				c.setName((String) obj);
				clazzes.add(c);
			}
		}
		return clazzes;
	}

	@Override
	public List<Dept> findDepts() {
		List objs = studentDao.list("dept");
		List<Dept> clazzes = new ArrayList<Dept>();
		for (Object obj : objs) {
			if (obj != null) {
				Dept c = new Dept();
				c.setName((String) obj);
				clazzes.add(c);
			}
		}
		return clazzes;
	}

	@Override
	public DataTablesResult<Student> getData(Integer draw, int pos, int rows) {
		DataTablesResult<Student> ads = new DataTablesResult<Student>();
		ads.setDraw(draw);
		ads.setData(studentDao.find(pos, rows));
		long total = studentDao.count();
		ads.setRecordsFiltered(total);
		ads.setRecordsTotal(total);
		return ads;
	}

	@Override
	public DataTablesResult<Student> getData(Integer draw, int pos, int rows, String dept, String clazz) {
		DataTablesResult<Student> ads = new DataTablesResult<Student>();
		Map<String, Object> maps = new HashMap<String, Object>();
		ads.setDraw(draw);
		long total = 0;
		if (clazz != null && !"".equals(clazz)) {
			maps.put("clazz", clazz);
			ads.setData(studentDao.find(pos, rows, maps));
			total = studentDao.count(maps);
		} else if (dept != null && !"".equals(dept)) {
			maps.put("dept", dept);
			ads.setData(studentDao.find(pos, rows, maps));
			total = studentDao.count(maps);
		} else {
			ads.setData(studentDao.find(pos, rows));
			total = studentDao.count();
		}
		ads.setRecordsFiltered(total);
		ads.setRecordsTotal(total);
		return ads;
	}
}
